package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IRespiteRepository;


import cn.tedu.antconlonysever.contract.pojo.entity.Respite;
import cn.tedu.antconlonysever.contract.pojo.param.RespiteAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.RespiteUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.RespiteListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.RespiteStandardVO;
import cn.tedu.antconlonysever.contract.service.IRespiteService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class RespiteServiceImpl implements IRespiteService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IRespiteRepository respiteRepository;
    @Override
    public void addNew(RespiteAddNewParam respiteAddNewParam) {
        log.debug("开始处理【添加流失缓存】的业务，参数：{}", respiteAddNewParam);
        Long lossId = respiteAddNewParam.getLossId();
        Integer count = respiteRepository.countLossId(lossId);
        if(count>0){
            String message = "添加流失缓存失败，流失缓存ID已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Respite respite=new Respite();
        BeanUtils.copyProperties(respiteAddNewParam,respite);
        respite.setGmtCreate(LocalDateTime.now());
        respite.setGmtModified(LocalDateTime.now());
        int rows = respiteRepository.insert(respite);
        if(rows!=1){
            String message = "添加流失缓存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的流失缓存数据插入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除流失缓存】的业务，参数：{}", id);

        RespiteStandardVO respite = respiteRepository.getStandardById(id);
        if(respite==null){
            String message = "删除流失缓存失败，缓存id不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = respiteRepository.deleteById(id);
        if(rows!=1){
            String message = "删除失缓存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }


    }

    @Override
    public void updateInfoById(Long id, RespiteUpdateParam respiteUpdateParam) {
        log.debug("开始处理【修改流失缓存】的业务，ID：{}，新数据：{}", id, respiteUpdateParam);
        RespiteStandardVO queryResult = respiteRepository.getStandardById(id);
        if(queryResult==null){
            String message = "修改缓存失败，缓存数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int count = respiteRepository.countLossIdNotId(id, respiteUpdateParam.getLossId());
        if (count>0) {
            String message = "修改缓存失败，尝试修改的流失缓存id已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Respite respite=new Respite();
        BeanUtils.copyProperties(respiteUpdateParam,respite);
        respite.setId(id);
        int rows = respiteRepository.update(respite);
        if(rows!=1){
            String message = "修改缓存失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public RespiteStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询缓存详情】的业务，ID：{}", id);
        RespiteStandardVO queryResult = respiteRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询缓存详情失败，缓存数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<RespiteListVO> list(Integer pageNum) {
        PageData<RespiteListVO> pageData = respiteRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<RespiteListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询缓存列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<RespiteListVO> pageData = respiteRepository.list(pageNum, pageSize);
        return pageData;
    }


}
